package com.yswl.yeeduo.ui.base;



import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yswl.yeeduo.R;

/**
 * 所有Fragment的基类（Fragment子类）继承自android.app.Fragment;
 * 1、负责将打开的Activity加入或移除注销管理器
 * 2、方便子类获得用户信息
 * 3、为标题栏提供内容设置，事件绑定
 */
public abstract class BasFragment extends Fragment {

	/**回退按钮**/
    ImageView mBack ;
    /**中间标题**/
    TextView mTitle;
    /**右边 **/
    ImageView mRight;


	protected View view;
	public Activity activity;
	
	public abstract Object loadView();

	public abstract void viewDidLoad();

	@Override
	public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.activity = (Activity) super.getActivity();
		try {
			this.view = (View) this.loadView();
			if (this.view == null) {
				throw new Exception();
			}
		} catch (Exception e) {
			this.view = inflater.inflate((Integer) this.loadView(), null);
		}
		
		this.viewDidLoad();
		return this.view;
	}
	

	public void finish() {
		activity.finish();
	}
	
	/** 设置标题栏*/
	protected void setNavTitle(String titleName){
		mTitle = (TextView)view.findViewById(R.id.nav_title);
		mBack = (ImageView) view.findViewById(R.id.nav_left);
		mRight = (ImageView) view.findViewById(R.id.nav_right);
		if (mTitle != null) {
			mTitle.setText(titleName);
		}
		if (mBack != null) {
			mBack.setVisibility(View.VISIBLE);
			mBack.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}


	
	/** 设置左侧导航*/
	protected void setNavLeft(OnClickListener listener) {
		mBack.setOnClickListener(listener);
	}
	
	/** 设置导航右*/
	protected void setNavRight(OnClickListener listener) {
		mRight.setOnClickListener(listener);
	}


	protected View findViewById(int __id) {
		try {
			return this.view.findViewById(__id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	
	
}
